[0.] Configuración inicial

Para replicar las secciones de esta clase, primero debe descargar el siguiente proyecto de R y abrir el archivo clase-04.Rproj.

0.1 Instalar/llamar las librerías de la clase

require(pacman) 
p_load(tidyverse,rio,skimr,viridis,
       gstat, #variogram
       sf, # Leer/escribir/manipular datos espaciales
       leaflet, # Visualizaciones dinámicas
       tmaptools, # geocode_OSM()
       nngeo, # st_nn function
       spdep, # Construct neighbours list from polygon list 
       osmdata) # Get OSM's data

0.2 Importar conjuntos de datos

## Inmuebles
houses <- import("input/house_prices.rds")
class(houses)
## [1] "data.frame"
skim(houses)
Data summary
Name houses
Number of rows 239343
Number of columns 27
_______________________
Column type frequency:
character 14
Date 3
numeric 10
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
id 0 1 24 24 0 239343 0
ad_type 0 1 9 9 0 1 0
l1 0 1 8 8 0 1 0
l2 0 1 9 15 0 4 0
l3 0 1 4 12 0 4 0
l4 0 1 0 22 73741 66 0
l5 0 1 0 22 146780 111 0
l6 0 1 0 22 208427 149 0
currency 0 1 0 3 132 3 0
price_period 0 1 0 7 233059 2 0
title 0 1 0 152 33 149209 0
description 0 1 0 9136 13 177846 0
property_type 0 1 2 15 0 10 0
operation_type 0 1 5 5 0 1 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
start_date 0 1 2019-04-30 2020-05-23 2019-11-28 363
end_date 0 1 2019-05-03 9999-12-31 2020-02-20 413
created_on 0 1 2019-04-30 2020-05-23 2019-11-28 363

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
V1 0 1.00 119672.00 6.909252e+04 1.00 59836.50 119672.00 179507.50 2.39343e+05 ▇▇▇▇▇
lat 0 1.00 5.73 2.460000e+00 3.22 4.62 4.71 6.23 1.25800e+01 ▇▂▁▁▂
lon 0 1.00 -74.95 9.600000e-01 -81.72 -75.60 -74.81 -74.07 -7.36000e+01 ▁▁▁▅▇
rooms 157341 0.34 3.27 1.860000e+00 1.00 3.00 3.00 3.00 4.00000e+01 ▇▁▁▁▁
bedrooms 152718 0.36 3.21 2.640000e+00 0.00 2.00 3.00 3.00 3.36000e+02 ▇▁▁▁▁
bathrooms 31736 0.87 2.86 1.530000e+00 1.00 2.00 2.00 4.00 2.00000e+01 ▇▁▁▁▁
surface_total 156169 0.35 415.50 3.567710e+03 -36.00 74.00 115.00 214.00 1.98000e+05 ▇▁▁▁▁
surface_covered 162288 0.32 517.83 4.578498e+04 1.00 73.00 112.00 200.00 1.20000e+07 ▇▁▁▁▁
price 0 1.00 841577426.63 3.183555e+09 0.00 253000000.00 420000000.00 794000000.00 8.50000e+11 ▇▁▁▁▁
n 0 1.00 86534.26 4.028687e+04 42700.00 51643.00 52877.00 129996.00 1.29996e+05 ▇▁▁▁▇
## dataframe to sf
houses <- st_as_sf(x = houses, ## datos
                   coords=c("lon","lat"), ## coordenadas
                   crs=4326) ## CRS
class(houses)
## [1] "sf"         "data.frame"
leaflet() %>% addTiles() %>% addCircleMarkers(data=houses[1:10,])
## Censo
mnz <- import("input/mgn_censo_2018.rds")

mnz
## Simple feature collection with 43361 features and 5 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.22353 ymin: 4.46292 xmax: -74.00074 ymax: 4.833006
## Geodetic CRS:  WGS 84
## First 10 features:
##                MANZ_CCNCT med_H_NRO_CUARTOS sum_HA_TOT_PER med_V_TOT_HOG
## 1  1100110000000064020323                 4            465             1
## 2  1100110000000064020919                 4            656             1
## 3  1100110000000064030215                NA             NA            NA
## 4  1100110000000064030217                 4             93             1
## 5  1100110000000063020410                 6             55             1
## 6  1100110000000063020203                 4            538             1
## 7  1100110000000063020239                 5            175             1
## 8  1100110000000063020517                 4            647             1
## 9  1100110000000063020513                 4            439             1
## 10 1100110000000063120117                 5            104             1
##    med_VA1_ESTRATO                       geometry
## 1                3 POLYGON ((-74.1378 4.677946...
## 2                3 POLYGON ((-74.13436 4.67957...
## 3               NA POLYGON ((-74.13465 4.68263...
## 4                3 POLYGON ((-74.1348 4.683005...
## 5                4 POLYGON ((-74.1215 4.663893...
## 6                4 POLYGON ((-74.11667 4.66107...
## 7                4 POLYGON ((-74.11826 4.66223...
## 8                4 POLYGON ((-74.11716 4.66048...
## 9                4 POLYGON ((-74.11905 4.66280...
## 10               4 POLYGON ((-74.1238 4.668334...
leaflet() %>% addTiles() %>% addPolygons(data=mnz[1:100,])
## Colegios
browseURL("https://datosabiertos.bogota.gov.co/dataset/resultados-pruebas-saber-11-bogota-d-c")

colegio <- st_read("input/col_saber_11.shp") %>% select(COD_DANE12,COLEGIO_SE,P_PUNTAJE)
## Reading layer `col_saber_11' from data source 
##   `/Users/eduard-martinez/Library/CloudStorage/Dropbox/teaching/bid-data-real-state/2023-01/clase-04/public/input/col_saber_11.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1138 features and 63 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.3642 ymin: 3.98618 xmax: -74.01831 ymax: 4.824433
## Geodetic CRS:  WGS 84
colegio
## Simple feature collection with 1138 features and 3 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.3642 ymin: 3.98618 xmax: -74.01831 ymax: 4.824433
## Geodetic CRS:  WGS 84
## First 10 features:
##     COD_DANE12                                 COLEGIO_SE P_PUNTAJE
## 1  3.11848e+11 COLEGIO DE INGLATERRA (THE ENGLISH SCHOOL)  348.0769
## 2  3.11848e+11                         GIMNASIO LOS PINOS  323.8462
## 3  3.11848e+11 COLEGIO DE NUESTRA SEÑORA DEL BUEN CONSEJO  320.3846
## 4  3.11848e+11                          GIMNASIO FEMENINO  341.5385
## 5  3.11848e+11                           COLEGIO PROVINMA  303.8462
## 6  3.11848e+11                COLEGIO MIGUEL ANTONIO CARO  285.0000
## 7  3.11848e+11                        COLEGIO SANTA MARIA  342.6923
## 8  3.11848e+11                    COLEGIO CARDENAL SANCHA  317.6923
## 9  3.11848e+11                   COLEGIO ANGLO COLOMBIANO  346.1538
## 10 3.11848e+11         COLEGIO ITALIANO LEONARDO DA VINCI  353.0769
##                      geometry
## 1   POINT (-74.03847 4.74956)
## 2  POINT (-74.03043 4.769851)
## 3  POINT (-74.04904 4.689974)
## 4   POINT (-74.02873 4.70749)
## 5  POINT (-74.03497 4.723775)
## 6  POINT (-74.03133 4.820195)
## 7  POINT (-74.03284 4.762669)
## 8  POINT (-74.05094 4.709756)
## 9  POINT (-74.04393 4.735146)
## 10 POINT (-74.05148 4.707325)
summary(colegio$P_PUNTAJE)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0   254.5   269.6   276.1   296.2   389.2
colegio <- colegio %>% subset(P_PUNTAJE>=quantile(P_PUNTAJE,0.9))
## CBD de Bogota
cbd <- geocode_OSM("Centro Internacional, Bogotá", as.sf=T) 

cbd
## Simple feature collection with 1 feature and 7 fields
## Active geometry column: point
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.07057 ymin: 4.613615 xmax: -74.07057 ymax: 4.613615
## Geodetic CRS:  WGS 84
##                          query      lat       lon  lat_min  lat_max   lon_min
## 1 Centro Internacional, Bogotá 4.613615 -74.07057 4.613373 4.613889 -74.07098
##     lon_max                           bbox                      point
## 1 -74.07023 POLYGON ((-74.07098 4.61337... POINT (-74.07057 4.613615)

0.3 Descargar datos de OSM

## parques
parques <- opq(bbox = getbb("Bogota Colombia")) %>%
           add_osm_feature(key = "leisure", value = "park") %>%
           osmdata_sf() %>% .$osm_polygons %>% select(osm_id,name)
leaflet() %>% addTiles() %>% addPolygons(data=parques)
leaflet() %>% addTiles() %>% addCircles(data=restaurantes)
leaflet() %>% addTiles() %>% addCircles(data=bancos)
leaflet() %>% addTiles() %>% addPolygons(data=mall)

[1.] Operaciones geometrías

Puede acceder a las viñetas de la librería sf así:

## Help
vignette("sf3")
## starting httpd help server ... done
vignette("sf4")

1.0. Afine transformations

st_crs(mnz) == st_crs(colegio) 
## [1] TRUE
st_crs(mnz) == st_crs(houses)
## [1] TRUE

1.1. Filtrar datos

## usando los valores de una variable
houses1 <- houses %>% subset(l3=="Bogotá D.C") %>% subset(l4=="Zona Chapinero")

leaflet() %>% addTiles() %>% addCircles(data=houses1)
## usando la geometría
chapinero <- getbb(place_name = "UPZ Chapinero, Bogota", 
                   featuretype = "boundary:administrative", 
                   format_out = "sf_polygon") %>% .$multipolygon
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
leaflet() %>% addTiles() %>% addPolygons(data=chapinero)
## crop puntos con poligono (opcion 1)
house_chapi <- st_crop(x = houses , y = chapinero) 
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
leaflet() %>% addTiles() %>% addPolygons(data=chapinero,col="red") %>% addCircles(data=house_chapi)
## crop puntos con poligono (opcion 2)
house_chapi <- st_intersection(x = houses , y = chapinero)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
leaflet() %>% addTiles() %>% addPolygons(data=chapinero,col="red") %>% addCircles(data=house_chapi)
## crop puntos con poligono (opcion 3)
house_chapi <- houses[chapinero,]

leaflet() %>% addTiles() %>% addPolygons(data=chapinero,col="red") %>% addCircles(data=house_chapi)
## crop poligonos con poligono
mnz_chapi <- mnz[chapinero,]

leaflet() %>% addTiles() %>% addPolygons(data=chapinero,col="red") %>% addPolygons(data=mnz_chapi)

1.2. Distancia a amenities

## Distancia a un punto
house_chapi$dist_cbd <- st_distance(x=house_chapi , y=cbd)

house_chapi$dist_cbd %>% head()
## Units: [m]
##          [,1]
## [1,] 7873.706
## [2,] 6073.111
## [3,] 8122.049
## [4,] 7770.387
## [5,] 7524.357
## [6,] 8209.241
## Distancia a muchos puntos
matrix_dist_cole <- st_distance(x=house_chapi , y=colegio)

matrix_dist_cole[1:5,1:5]
## Units: [m]
##          [,1]     [,2]     [,3]      [,4]     [,5]
## [1,] 7962.075 10280.33 3549.903  9458.105 6362.623
## [2,] 9521.528 11901.43 5382.668 11065.930 7851.046
## [3,] 7736.599 10047.12 3302.069  9226.568 6147.381
## [4,] 7769.670 10164.84 3914.930  9326.110 6086.893
## [5,] 8299.952 10624.61 3900.579  9801.072 6691.346
min_dist_cole <- apply(matrix_dist_cole , 1 , min)

min_dist_cole %>% head()
## [1]  593.8679  291.5314  483.5662  981.5659  880.7624 1354.2629
house_chapi$dist_cole = min_dist_cole

## Distancia a muchos polygonos
matrix_dist_parque <- st_distance(x=house_chapi , y=parques)

matrix_dist_parque[1:5,1:5]
## Units: [m]
##          [,1]     [,2]     [,3]     [,4]     [,5]
## [1,] 1899.274 5713.387 12584.21 12617.14 9334.131
## [2,] 1030.924 4168.137 10822.57 10737.20 8105.307
## [3,] 2080.784 5897.597 12792.51 12863.28 9466.137
## [4,] 1090.562 4746.753 11732.55 12261.21 8160.210
## [5,] 1714.653 5499.423 12322.95 12279.73 9200.549
mean_dist_parque <- apply(matrix_dist_parque , 1 , mean)

mean_dist_parque %>% head()
## [1] 11533.81 10656.81 11636.02 10822.41 11423.99 10677.08
house_chapi$dist_parque = mean_dist_parque

3.3. Unir objetos usando la geometría

## definir sub-muestra
new_chapi <- house_chapi[st_buffer(house_chapi[100,],200),]

leaflet() %>% addTiles() %>%
addPolygons(data=mnz_chapi[new_chapi,],col="red") %>%
addCircles(data=new_chapi)
## unir dos conjuntos de datos basados en la distancia
new_chapi <- st_join(new_chapi, mnz_chapi[new_chapi,] , join = st_nn , maxdist = 20 , k = 1 , progress = FALSE)
## lines or polygons
colnames(new_chapi)
##  [1] "V1"                "id"                "ad_type"          
##  [4] "start_date"        "end_date"          "created_on"       
##  [7] "l1"                "l2"                "l3"               
## [10] "l4"                "l5"                "l6"               
## [13] "rooms"             "bedrooms"          "bathrooms"        
## [16] "surface_total"     "surface_covered"   "price"            
## [19] "currency"          "price_period"      "title"            
## [22] "description"       "property_type"     "operation_type"   
## [25] "n"                 "dist_cbd"          "dist_cole"        
## [28] "dist_parque"       "MANZ_CCNCT"        "med_H_NRO_CUARTOS"
## [31] "sum_HA_TOT_PER"    "med_V_TOT_HOG"     "med_VA1_ESTRATO"  
## [34] "geometry"
leaflet() %>% addTiles() %>% 
addPolygons(data=mnz_chapi[new_chapi,] , col="red" , label=mnz_chapi[new_chapi,]$MANZ_CCNCT) %>% 
addCircles(data=new_chapi , label=new_chapi$MANZ_CCNCT)
## unir dos conjuntos de datos basados en la geometría
house_chapi <- st_join(x=house_chapi , y=mnz_chapi)
colnames(house_chapi)
##  [1] "V1"                "id"                "ad_type"          
##  [4] "start_date"        "end_date"          "created_on"       
##  [7] "l1"                "l2"                "l3"               
## [10] "l4"                "l5"                "l6"               
## [13] "rooms"             "bedrooms"          "bathrooms"        
## [16] "surface_total"     "surface_covered"   "price"            
## [19] "currency"          "price_period"      "title"            
## [22] "description"       "property_type"     "operation_type"   
## [25] "n"                 "dist_cbd"          "dist_cole"        
## [28] "dist_parque"       "MANZ_CCNCT"        "med_H_NRO_CUARTOS"
## [31] "sum_HA_TOT_PER"    "med_V_TOT_HOG"     "med_VA1_ESTRATO"  
## [34] "geometry"

[2.] Recuperar información de las covariables

## stringr
browseURL("https://evoldyn.gitlab.io/evomics-2018/ref-sheets/R_strings.pdf")

## example
word = "Hola mundo, hoy es 19 de julio de 2022"

## Detect Matches
str_detect(string = word , pattern = "19") ## Detect the presence of a pattern match
## [1] TRUE
str_locate(string = word , pattern = "19") ##  Locate the positions of pattern matches in a string
##      start end
## [1,]    20  21
str_count(string = word , pattern = "o") ## Count the number of matches in a string
## [1] 4
## Subset Strings
str_extract(string = word , pattern = "19") ## Return the first pattern match found in each strin
## [1] "19"
str_match(string = word , pattern = "19") ## Return the first pattern match found in each string
##      [,1]
## [1,] "19"
str_sub(string = word , start = 1, end = 4)
## [1] "Hola"
## Mutate Strings
str_replace(string = word , pattern = "19" , replacement = "10+9")
## [1] "Hola mundo, hoy es 10+9 de julio de 2022"
str_to_lower(string = word)
## [1] "hola mundo, hoy es 19 de julio de 2022"
str_to_upper(string = word)
## [1] "HOLA MUNDO, HOY ES 19 DE JULIO DE 2022"
## Regular Expressions
str_replace_all(string = word , pattern = " " , replacement = "-")
## [1] "Hola-mundo,-hoy-es-19-de-julio-de-2022"
str_replace_all(string = word , "[:blank:]" , replacement = "-")
## [1] "Hola-mundo,-hoy-es-19-de-julio-de-2022"
str_replace_all(string = word , "19|2022" , replacement = "-")
## [1] "Hola mundo, hoy es - de julio de -"
str_replace_all(string = word , "[0-9]" , replacement = "-")
## [1] "Hola mundo, hoy es -- de julio de ----"
## aplicacion
house_chapi$description <- tolower(house_chapi$description)

house_chapi$surface_total[49] ## not surface_total
## [1] NA
house_chapi$surface_covered[49] ## not surface_covered
## [1] NA
house_chapi$description[49] ## explore description
## [1] "remodelado area 136 mts, piso 2 exterior, 3 habitaciones, 2 baños completos, sala comedor con chimenea, cocina cerrada, zona de lavanderia, cuarto y baño de servicio, 2 garajes, deposito. edificio con jardin y parque para niños. apts c-dos \n"
x <- "[:space:]+[:digit:]+[:space:]+mts" ## pattern

str_locate_all(string = house_chapi$description[49] , pattern = x) ## detect pattern
## [[1]]
##      start end
## [1,]    16  23
str_extract(string=house_chapi$description[49] , pattern= x) ## extrac pattern
## [1] " 136 mts"
## make new var
house_chapi <- house_chapi %>% 
               mutate(new_surface = str_extract(string=house_chapi$description , pattern= x))
table(house_chapi$new_surface) %>% head()
## 
## \n\n    170 mts      \n 105 mts      \n 125 mts      \n 134 mts      \n 162 mts 
##               1               1               1               1               1 
##      \n 163 mts 
##               1
## another pattern
y = "[:space:]+[:digit:]+[:space:]+m2"
house_chapi = house_chapi %>% 
              mutate(new_surface = ifelse(is.na(new_surface)==T,
                                          str_extract(string=house$description , pattern= y),
                                          new_surface))
table(house_chapi$new_surface) %>% head()
## 
## \n\n    170 mts      \n 105 mts      \n 125 mts      \n 134 mts      \n 162 mts 
##               1               1               1               1               1 
##      \n 163 mts 
##               1

[3.] Dependencia espacial

Tomado de: Aplied Spatial Data in R

Donde Z(s) es el valor de la variable aleatorio Z en la ubicacion s. Mientras que h es la distancia euclidiana entre los valores de la variable Z

## motivation
import("output/variograma.rds")

## sf to sp
house_chapi_sp <- house_chapi %>% as_Spatial()
house_chapi_sp
## class       : SpatialPointsDataFrame 
## features    : 15331 
## extent      : -74.06801, -74.01063, 4.625, 4.686407  (xmin, xmax, ymin, ymax)
## crs         : +proj=longlat +datum=WGS84 +no_defs 
## variables   : 34
## names       :     V1,                       id,   ad_type, start_date, end_date, created_on,       l1,           l2,         l3,            l4,      l5,       l6, rooms, bedrooms, bathrooms, ... 
## min values  :     12, //515Rnb83C1fBw3/vGM4w==, Propiedad,      18016,    18019,      18016, Colombia, Cundinamarca, Bogotá D.C, Puente Aranda,        ,         ,     1,        0,         1, ... 
## max values  : 239333, ZZy4xHQ/GI+0j7UI/8uGog==, Propiedad,      18405,  2932896,      18405, Colombia, Cundinamarca, Bogotá D.C,    Zona Norte, Usaquén, San Luis,    28,      336,        20, ...

En el caso de observaciones atípicas, los valores extremos de nubes de variogramas se identifican fácilmente para encontrar los valores atípicos. Es posible que sea necesario eliminarlos o, de lo contrario, se pueden calcular medidas sólidas para el variograma de muestra pasando el argumento lógico cressie = TRUE a la llamada a la función del variograma (Cressie, 1993).

## estimations
variogram(price/1000000 ~ 1, house_chapi_sp, cloud = F , cressie=T) %>% plot()

house_chapi_sp$normal <- rnorm(n = nrow(house_chapi_sp),
                               mean = mean(house_chapi_sp$price/1000000),
                               sd = 1000)
  
db_plot = left_join(x = variogram(price/1000000 ~ 1, house_chapi_sp, cloud = F , cressie=T) %>% mutate(estimate=gamma) %>% select(dist,estimate),
                    y = variogram(normal ~ 1, house_chapi_sp, cloud = F , cressie=T) %>% mutate(normal=gamma) %>% select(dist,normal),"dist") 

db_plot %>% head()
##        dist  estimate  normal
## 1 0.1265643  821338.1 1004519
## 2 0.3181829  997126.6 1009998
## 3 0.5218676 1106718.0 1008817
## 4 0.7260559 1173394.6 1012289
## 5 0.9324794 1292019.7 1007428
## 6 1.1382918 1372392.7 1005828
plot = ggplot(db_plot) + 
geom_point(aes(x=dist, y=normal , fill="Datos aleatorios (Dist. Normal)"), shape=21, alpha=0.5, size=5 ) +
geom_point(aes(x=dist, y=estimate , fill="Precio de la vivienda (properati)"), shape=21, alpha=0.5, size=5 ) +
labs(caption = "Fuente: Properati", y = "Semivariograma", x = "Distancia de separación entre inmuebles", fill = "") + theme_bw()
#ggsave(plot,"output/variograma.png")

[4.] Vecinos espaciales

4.1 vecinos en la manzana

house_chapi = house_chapi %>%
              group_by(MANZ_CCNCT) %>%
              mutate(new_surface_2=median(surface_total,na.rm=T))

table(is.na(house_chapi$surface_total))
## 
## FALSE  TRUE 
##  4655 10676
table(is.na(house_chapi$surface_total),
      is.na(house_chapi$new_surface_2)) # ahora solo tenemos menos missing values
##        
##         FALSE  TRUE
##   FALSE  4655     0
##   TRUE  10179   497

4.2 Vecinos mas cercanos

## definir submuestra
new_chapi <- house_chapi[st_buffer(house_chapi[100,],200),]

new_chapi
## Simple feature collection with 436 features and 35 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.05035 ymin: 4.659747 xmax: -74.04698 ymax: 4.663025
## Geodetic CRS:  WGS 84
## # A tibble: 436 × 36
## # Groups:   MANZ_CCNCT [11]
##       V1 id     ad_type start_date end_date   created_on l1    l2    l3    l4   
##    <int> <chr>  <chr>   <date>     <date>     <date>     <chr> <chr> <chr> <chr>
##  1  1782 yx2+P… Propie… 2019-11-20 2020-02-14 2019-11-20 Colo… Cund… Bogo… Zona…
##  2  1792 cyow5… Propie… 2019-11-20 2019-11-27 2019-11-20 Colo… Cund… Bogo… Zona…
##  3  1812 sjLUd… Propie… 2019-11-20 2020-01-07 2019-11-20 Colo… Cund… Bogo… Zona…
##  4  2101 mVO03… Propie… 2019-07-18 2020-01-07 2019-07-18 Colo… Cund… Bogo… Zona…
##  5  2996 lf3tM… Propie… 2019-05-15 2019-06-08 2019-05-15 Colo… Cund… Bogo… Zona…
##  6  3510 XZQrn… Propie… 2019-10-24 9999-12-31 2019-10-24 Colo… Cund… Bogo… Zona…
##  7  3524 NS623… Propie… 2019-10-24 2020-01-07 2019-10-24 Colo… Cund… Bogo… Zona…
##  8  4919 VkaHu… Propie… 2019-09-23 2020-01-07 2019-09-23 Colo… Cund… Bogo… Zona…
##  9  8384 +eyO0… Propie… 2019-05-17 2019-11-06 2019-05-17 Colo… Cund… Bogo… Zona…
## 10  8385 21OyA… Propie… 2019-05-17 2019-06-08 2019-05-17 Colo… Cund… Bogo… Zona…
## # … with 426 more rows, and 26 more variables: l5 <chr>, l6 <chr>, rooms <int>,
## #   bedrooms <int>, bathrooms <int>, surface_total <int>,
## #   surface_covered <int>, price <dbl>, currency <chr>, price_period <chr>,
## #   title <chr>, description <chr>, property_type <chr>, operation_type <chr>,
## #   n <int>, dist_cbd [m], dist_cole <dbl>, dist_parque <dbl>,
## #   MANZ_CCNCT <chr>, med_H_NRO_CUARTOS <dbl>, sum_HA_TOT_PER <int>,
## #   med_V_TOT_HOG <dbl>, med_VA1_ESTRATO <dbl>, geometry <POINT [°]>, …
## obtener objeto sp
house_chapi_poly <- new_chapi %>% st_buffer(20) %>% as_Spatial() # poligonos

## obtener vecinos
nb_chapi = poly2nb(pl=house_chapi_poly , queen=T) # opcion reina

## vecinos del inmueble 29
nb_chapi[[29]]
## [1]  18  48 181
leaflet() %>% addTiles() %>% 
addCircles(data=new_chapi[29,],col="red") %>% 
addCircles(data=new_chapi[nb_chapi[[29]],])

Veamos un ejemplo para un vecino y comparemos los resultados.

Referencias

  • Lovelace, R., Nowosad, J., & Muenchow, J. (2019). Geocomputation with R. [Ver aquí]

    • Cap. 4: Spatial Data Operations
    • Cap. 5: Geometry Operations
    • Cap. 6: Reprojecting geographic data
    • Cap. 11: Statistical learning
  • Bivand, R. S., Pebesma, E. J., Gómez-Rubio, V., & Pebesma, E. J. (2013). Applied spatial data analysis with R. [Ver aquí]

    • Cap. 4: Spatial Data Import and Export
    • Cap. 7: Spatial Point Pattern Analysis
    • Cap. 8: Interpolation and Geostatistics